package com.example.myzhugezhaofang.fragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.myhttplibrary.Bean.BannerBean;
import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.activity.NewHouseActivity;
import com.example.myzhugezhaofang.activity.WebActivity;
import com.example.myzhugezhaofang.adapter.FragPagerAdapter;
import com.example.myzhugezhaofang.adapter.HighOpinionAdapter;
import com.example.myzhugezhaofang.adapter.KolimagesAdapter;
import com.example.myzhugezhaofang.adapter.LikeAdapter;
import com.example.myzhugezhaofang.adapter.MarketAdapter;
import com.example.myzhugezhaofang.adapter.NewsHotsAdapter;
import com.example.myzhugezhaofang.bean.BottomBannerBean;
import com.example.myzhugezhaofang.bean.HeadLinesBean;
import com.example.myzhugezhaofang.bean.HighOpinionBean;
import com.example.myzhugezhaofang.bean.KolimagesBean;
import com.example.myzhugezhaofang.bean.LikeBean;
import com.example.myzhugezhaofang.bean.MarketBean;
import com.example.myzhugezhaofang.bean.NavigationBean;
import com.example.myzhugezhaofang.bean.NewHotsBean;
import com.example.myzhugezhaofang.bean.TopicBean;
import com.example.myzhugezhaofang.callback.Mainconcations;
import com.example.myzhugezhaofang.model.MainModel;
import com.example.myzhugezhaofang.presenter.MainPresenter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.sunfusheng.marqueeview.MarqueeView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager;

/**
 * 作者：朱勇闯
 * data : 2021/4/10  14:30
 * 作用： xxxx
 */
public class HomeFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView<T>, OnBannerListener {
    @BindView(R.id.banner_home)
    Banner banner_home;
    @BindView(R.id.btn_city_home)
    Button btn_city_home;
    @BindView(R.id.btn_search_home)
    Button btn_search_home;
    @BindView(R.id.vp_navigation)
    GridViewPager vp_navigation;
    @BindView(R.id.showHighOpinion)
    RecyclerView rv_HighOpinion;
    @BindView(R.id.tv_first_navigation)
    TextView tv_first_navigation;
    @BindView(R.id.tv_Last_navigation)
    TextView tv_Last_navigation;
    @BindView(R.id.showkolimages)
    RecyclerView rv_kolimages;
    @BindView(R.id.showLike)
    RecyclerView rv_like;
    @BindView(R.id.rv_market)
    RecyclerView rv_market;
    @BindView(R.id.showNewsHots)
    RecyclerView rv_newsHots;
    @BindView(R.id.iv_bottomBanner)
    ImageView iv_bottomBanner;
    @BindView(R.id.iv_topic_left_1)
    ImageView iv_topic_1;
    @BindView(R.id.iv_topic_top_2)
    ImageView iv_topic_2;
    @BindView(R.id.iv_topic_bottom_3)
    ImageView iv_topic_3;
    @BindView(R.id.iv_topic_bottom_4)
    ImageView iv_topic_4;
    @BindView(R.id.tv_marqueeView_home)
    MarqueeView tv_marqueeView_home;
    private String TAG = "朱勇闯Home";
    private List<HighOpinionBean.ListBean> highOpinionList;
    private List<NavigationBean.ListBean> navigationList;
    private List<BannerBean.ListBean> bannerList;
    private List<HeadLinesBean.ListBean> headList;
    private List<LikeBean.ListBean> likeList;
    private List<NewHotsBean.ListBean> newHotsList;
    private List<KolimagesBean.ListnewsBean> kolimagesList;
    private List<TopicBean.ListBean> topicList;
    private HighOpinionAdapter highOpinionAdapter;
    private NewsHotsAdapter newsHotsAdapter;
    private LikeAdapter likeAdapter;
    private KolimagesAdapter kolimagesAdapter;
    private FragmentPagerAdapter navigationAdapter;
    private MarketAdapter marketAdapter;
    private MarketBean.ListBean marketList;
    private String url;
    private Intent intent;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
//HomeBanner
        HashMap<String, Object> mapBanner = new HashMap<>();
        mapBanner.put("cityid", "1");
        mapBanner.put("platformType", "4");
        mPresenter.getData(mapBanner, "https://api.zhuge.com/home/v1/Appindex/", "showBanner", 0);
//Navigation
        HashMap<String, Object> mapNavigation = new HashMap<>();
        mapNavigation.put("cityid", "1");
        mapNavigation.put("platformType", "4");
        mPresenter.getData(mapNavigation, "https://api.zhuge.com/home/v1/Appindex/", "showNavigation", 1);
//News头条
        HashMap<String, Object> mapHead = new HashMap<>();
        mapHead.put("cityid", "1");
        mapHead.put("platformType", "4");
        mPresenter.getData(mapHead, "https://api.zhuge.com/home/v1/Appindex/", "showNews", 2);
//热门主题
        HashMap<String, Object> mapTopic = new HashMap<>();
        mapTopic.put("cityid", "1");
        mapTopic.put("platformType", "4");
        mPresenter.getData(mapTopic, "https://api.zhuge.com/home/v1/Appindex/", "showTopic", 3);
//北京站
        HashMap<String, Object> mapKolimages = new HashMap<>();
        mapKolimages.put("cityid", "1");
        mapKolimages.put("platformType", "4");
        mPresenter.getData(mapKolimages, "https://api.zhuge.com/home/v1/Appindex/", "showKolimages", 4);
//诸葛行情
        HashMap<String, Object> mapMarket = new HashMap<>();
        mapMarket.put("cityid", "1");
        mapMarket.put("city", "bj");
        mapMarket.put("platformType", "4");
        mPresenter.getData(mapMarket, "https://api.zhuge.com/home/v1/Appindex/", "showZhuge", 5);

//热门资讯
        HashMap<String, Object> mapNewHots = new HashMap<>();
        mapNewHots.put("cityid", "1");
        mapNewHots.put("platformType", "4");
        mPresenter.getData(mapNewHots, "https://api.zhuge.com/home/v1/Appindex/", "showNewsHots", 6);
//口碑楼盘
        HashMap<String, Object> mapHighOpinion = new HashMap<>();
        mapHighOpinion.put("cityid", "1");
       /* map.put("platformType", "4");
        map.put("cityid", "1");
        map.put("ci operaVersiontyid", "6.0.1");
        map.put("s_screen_width", "810");
        map.put("token", "kMgo8gS1Bfl4l5fFjlbpKmLBJtVKmRpwVA1w1QLjspxGZl90V2vhjM2tIAGC-sCufVL73KbLC5ZtVncUfXP_4A==s_distinct_id3898309");
        map.put("city", "bj");
        map.put("app_name", "zgzf");
        map.put("s_manufacturer", "Netease");
        map.put("appVersion", "4.5.9.0");
        map.put("imei", "330000000178567");
        map.put("s_ip", "10.0.2.15");
        map.put("s_wifi", "true");
        map.put("platform_type", "4");
        map.put("s_os", "Android");
        map.put("mac", "08:00:27:2B:63:9D");
        map.put("appName", "zgzf");
        map.put("user_token", "kMgo8gS1Bfl4l5fFjlbpKmLBJtVKmRpwVA1w1QLjspxGZl90V2vhjM2tIAGC-sCufVL73KbLC5ZtVncUfXP_4A==");
        map.put("platformType", "4");
        map.put("user_type", "C端用户");
        map.put("ip", "4.5.9.0");
        map.put("device_only_id", "ffffffff-a46f-4eef-ffff-ffffc2e834d9");
        map.put("appSignatures", "appSignatures");
        map.put("s_os_version", "6.0.1");
        map.put("device_from", "4");
        map.put("s_screen_height", "1440");
        map.put("s_model", "MuMu");*/
        mPresenter.getData(mapHighOpinion, "https://api.zhuge.com/home/v1/Appindex/", "showHighOpinion", 7);
//猜你喜欢
        HashMap<String, Object> mapLike = new HashMap<>();
        mapLike.put("cityid", "1");
        mapLike.put("platformType", "4");
        mPresenter.getData(mapLike, "https://api.zhuge.com/home/v1/Appindex/", "showLike", 8);
//底部banner
        HashMap<String, Object> mapshowBottom = new HashMap<>();
        mapshowBottom.put("cityid", "1");
        mapshowBottom.put("platformType", "4");
        mPresenter.getData(mapshowBottom, "https://api.zhuge.com/home/v1/Appindex/", "showBottom", 9);
    }


    @Override
    protected int createLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }

    @Override
    public void showList(Object t, int type) {
        if (type == 0) {
            BannerBean bannerBean = new Gson().fromJson((JsonElement) t, BannerBean.class);
            bannerList = bannerBean.getList();
            ArrayList<String> ImageUrls = new ArrayList<>();
            for (int i = 0; i < bannerList.size(); i++) {
                ImageUrls.add(bannerList.get(i).getThumb());
            }
            banner_home.setImages(ImageUrls)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            Glide.with(context).load(path).into(imageView);
                        }
                    }).setIndicatorGravity(BannerConfig.RIGHT)
                    .setOnBannerListener(this).start();
        }
        if (type == 1) {
            NavigationBean navigation = new Gson().fromJson((JsonElement) t, NavigationBean.class);
            navigationList = navigation.getList();
            vp_navigation.setDataAllCount(navigationList.size())// 设置数量总条数
                    // 设置item的纵向间距
                    // 设置图片宽度
                    .setImageWidth(40)
                    // 设置图片高度
                    .setImageHeight(40)
                    // 设置文字与图片的间距
                    .setTextImgMargin(10)
                    // 设置文字颜色
                    .setTextColor(ContextCompat.getColor(getContext(), R.color.black))
                    // 设置文字大小
                    .setTextSize(11)
                    // 设置每页行数
                    .setRowCount(2)
                    // 设置每页列数
                    .setColumnCount(5)
                    // 设置指示器的item宽度
                    .setPointChildWidth(5)
                    // 设置指示器的item高度
                    .setPointChildHeight(5)
                    // 设置指示器的item的间距
                    .setPointChildMargin(10)
                    // 设置指示器与page的间距
                    .setPointMarginPage(18)
                    // 指示器的item是否为圆形，默认圆形直径取宽高的最小值
                    .setPointIsCircle(true)
                    // 指示器item未选中的颜色
                    .setPointNormalColor(ContextCompat.getColor(getContext(), R.color.gray))
                    // 指示器item选中的颜色
                    .setPointSelectColor(ContextCompat.getColor(getContext(), R.color.primary_color))
                    // 数据绑定
                    .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() {
                        @Override
                        public void displayImageText(ImageView imageView, TextView textView, int position) {
                            // 自己进行数据的绑定，灵活度更高，不受任何限制
                            Glide.with(context).load(navigationList.get(position).getIcon()).into(imageView);
                            textView.setText(navigationList.get(position).getTitle());
                        }
                    })
                    // Item点击
                    .setGridItemClickListener(new GridViewPager.GridItemClickListener() {
                        @Override
                        public void click(int position) {
                          /*  if (position == 0 || position == 13) {
                                intent = new Intent(getContext(), NewHouseActivity.class);
                                startActivity(intent);
                            } else if (position == 3 || position == 5 || position == 6 || position == 7 ||
                                    position == 8 || position == 9 || position == 10) {
                                url = navigationList.get(position).getUrl();
                                intent = new Intent(getContext(), WebActivity.class);
                                intent.putExtra("link", url);
                                startActivity(intent);
                            } else if (position == 12) {

                            }*/
                            //webView
                            if (position == 0 || position == 13 || position == 14
                                    || position == 15 || position == 16) {
                                intent = new Intent(getContext(), NewHouseActivity.class);
                                startActivity(intent);
                            } else if (position == 3 || position == 5 || position == 6 || position == 7
                                    || position == 8 || position == 9 || position == 10 || position == 11) {
                                url = navigationList.get(position).getUrl();
                                intent = new Intent(getContext(), WebActivity.class);
                                intent.putExtra("link", url);
                                startActivity(intent);
                            }//二手房
                            else if (position == 1) {

                            }//咨询
                            else if (position == 12) {

                            }//找小区、租房
                            else if (position == 4 || position == 2) {

                            } else {
                                Toast.makeText(mActivity, "版本暂未更新,亲们请等待", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).show();
//            initNavigation();
//            navigationAdapter.notifyDataSetChanged();
        }
        if (type == 2) {
//            headList = new ArrayList<>();
            HeadLinesBean headLinesBean = new Gson().fromJson((JsonElement) t, HeadLinesBean.class);
            headList = headLinesBean.getList();
            List<String> info = new ArrayList<>();
            for (int i = 0; i < headList.size(); i++) {
                info.add(headList.get(i).getTitle());
            }
            tv_marqueeView_home.startWithList(info);
            // 在代码里设置自己的动画
           /* String notice = "心中有阳光，脚底有力量！心中有阳光，脚底有力量！心中有阳光，脚底有力量！";
            tv_marqueeView_home.startWithText(notice);*/
            // 在代码里设置自己的动画
            tv_marqueeView_home.startWithList(info, R.anim.anim_bottom_in, R.anim.anim_top_out);
            tv_marqueeView_home.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
                @Override
                public void onItemClick(int position, TextView textView) {
                    String url = headList.get(position).getUrl();
                    Intent intent = new Intent(getContext(), WebActivity.class);
                    intent.putExtra("link", url);
                    startActivity(intent);
                }
            });
        }

        if (type == 3) {
            topicList = new ArrayList<>();
            TopicBean topicBean = new Gson().fromJson((JsonElement) t, TopicBean.class);
            topicList = topicBean.getList();
            Glide.with(context).load(topicList.get(0).getThumb()).into(iv_topic_1);
            Glide.with(context).load(topicList.get(1).getThumb()).into(iv_topic_2);
            Glide.with(context).load(topicList.get(2).getThumb()).into(iv_topic_3);
            Glide.with(context).load(topicList.get(3).getThumb()).into(iv_topic_4);
        }

        if (type == 4) {
            KolimagesBean kolimagesBean = new Gson().fromJson((JsonElement) t, KolimagesBean.class);
            kolimagesList = kolimagesBean.getListnews();
            kolimagesAdapter = new KolimagesAdapter(kolimagesList, context, R.layout.item_kolimages);
            rv_kolimages.setLayoutManager(new LinearLayoutManager(context));
            rv_kolimages.setAdapter(kolimagesAdapter);
            kolimagesAdapter.notifyDataSetChanged();
        }
        if (type == 5) {
            MarketBean marketBean = new Gson().fromJson((JsonElement) t, MarketBean.class);
            marketList = marketBean.getList();
            Log.e(TAG, "showList: " + marketList.getHouse_drop_title());
            marketAdapter = new MarketAdapter(marketList, context);
            rv_market.setLayoutManager(new LinearLayoutManager(context));
            rv_market.setAdapter(marketAdapter);
            rv_market.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
            marketAdapter.notifyDataSetChanged();
        }
        if (type == 6) {
            newHotsList = new ArrayList<>();
            NewHotsBean newHotsBean = new Gson().fromJson((JsonElement) t, NewHotsBean.class);
            newHotsList = newHotsBean.getList();
            newsHotsAdapter = new NewsHotsAdapter(newHotsList, context, R.layout.item_newhots);
            rv_newsHots.setLayoutManager(new LinearLayoutManager(context));
            rv_newsHots.setAdapter(newsHotsAdapter);
            newsHotsAdapter.notifyDataSetChanged();

        }

        if (type == 7) {
            HighOpinionBean highOpinionBean = new Gson().fromJson((JsonElement) t, HighOpinionBean.class);
            highOpinionList = highOpinionBean.getList();
            highOpinionAdapter = new HighOpinionAdapter(highOpinionList, context, R.layout.item_highopinion);
            rv_HighOpinion.setLayoutManager(new LinearLayoutManager(context));
            rv_HighOpinion.setAdapter(highOpinionAdapter);
            rv_HighOpinion.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
            highOpinionAdapter.notifyDataSetChanged();
        }
        if (type == 8) {
            LikeBean likeBean = new Gson().fromJson((JsonElement) t, LikeBean.class);
            likeList = likeBean.getList();
            likeAdapter = new LikeAdapter(likeList, context, R.layout.item_like);
            rv_like.setLayoutManager(new LinearLayoutManager(context));
            rv_like.setAdapter(likeAdapter);
            rv_like.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
            likeAdapter.notifyDataSetChanged();
        }
        if (type == 9) {
            BottomBannerBean bottomBannerBean = new Gson().fromJson((JsonElement) t, BottomBannerBean.class);
            Glide.with(context).load(bottomBannerBean.getThumb()).into(iv_bottomBanner);
            iv_bottomBanner.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), NewHouseActivity.class);
                    startActivity(intent);
                }
            });
        }
    }

    private void initNavigation() {
        ArrayList<NavigationBean.ListBean> navigationList1 = new ArrayList();
        ArrayList<NavigationBean.ListBean> navigationList2 = new ArrayList<>();
        for (int i = 0; i < navigationList.size(); i++) {
            if (i < 10) {
                navigationList1.add(navigationList.get(i));
            } else {
                navigationList2.add(navigationList.get(i));
            }
        }
        NavigationFragment navigationItemFragment1 = NavigationFragment.newInstance(navigationList1);
        NavigationFragment navigationItemFragment2 = NavigationFragment.newInstance(navigationList2);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(navigationItemFragment1);
        fragments.add(navigationItemFragment2);
        navigationAdapter = new FragPagerAdapter(getChildFragmentManager(), fragments);
       /* vp_navigation.setAdapter(navigationAdapter);
        vp_navigation.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tv_first_navigation.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                    tv_Last_navigation.setBackgroundColor(getResources().getColor(android.R.color.background_dark));
                } else {
                    tv_first_navigation.setBackgroundColor(getResources().getColor(android.R.color.background_dark));
                    tv_Last_navigation.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });*/
    }

    @Override
    public void showError(String error) {
        Log.e(TAG, "showError: " + error);
    }

    @Override
    public void OnBannerClick(int position) {
        String url = bannerList.get(position).getUrl();
        Intent intent = new Intent(getActivity(), WebActivity.class);
        intent.putExtra("link", url);
        startActivity(intent);
    }


}
